package com.cpi.cornpp.purchaseorder.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.biweek.exception.BiWeekException;
import com.cpi.cornpp.biweeks.services.BiWeekPeriodVersionService;
import com.cpi.cornpp.biweeks.view.BiWeekPeriodVersionView;
import com.cpi.cornpp.blockdistribution.exception.BlockDistributionException;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.manualcharges.view.ManualChargesFilterSearchView;
import com.cpi.cornpp.monthsperiods.dao.MonthsPeriodsDAO;
import com.cpi.cornpp.monthsperiods.view.MonthsPeriodsForVersionDTO;
import com.cpi.cornpp.plants.exception.PlantException;
import com.cpi.cornpp.plants.services.PlantService;
import com.cpi.cornpp.plants.view.PlantView;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.comparator.PurchaseOrderVersionComparator;
import com.cpi.cornpp.purchaseorder.converter.PurchaseOrderVersionConverter;
import com.cpi.cornpp.purchaseorder.dao.PurchaseOrderVersionDAO;
import com.cpi.cornpp.purchaseorder.dto.PurchaseOrderVersionDTO;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderCostVersionView;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;
import com.cpi.cornpp.tagdistribution.exception.TagDistributionException;
import com.cpi.cornpp.tagdistribution.service.TagDistributionService;
import com.cpi.cornpp.tagdistribution.view.TagDistributionView;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.service.TransportService;

public class PurchaseOrderVersionService {
	public int addPurchaseOrderVersion(PurchaseOrderVersionView povView) throws PurchaseProgramException{
		int insertedPovId = -1;
		PurchaseOrderCostVersionView pcvView = null;
		PurchaseOrderVersionDTO povDTO = null;
		PurchaseOrderVersionDAO povDAO = null;
		PurchaseOrderCostVersionService pcvService = null;
		
		try{
			
			if (povView != null){
				povDAO = new PurchaseOrderVersionDAO();
				povDTO = PurchaseOrderVersionConverter.convert(povView);
				insertedPovId = povDAO.insertPurchaseOrderVersion(povDTO);
				
				if (CollectionUtils.isNotEmpty(povView.getPurchaseOrderCostVersions())){
					pcvService = new PurchaseOrderCostVersionService();
					
					for (Iterator itr = povView.getPurchaseOrderCostVersions().iterator(); itr.hasNext(); ){
						pcvView = (PurchaseOrderCostVersionView) itr.next();
						pcvView.setPurchaseOrderVersionId(new Integer(insertedPovId));
					}
					
					pcvService.addPurchaseOrderCostVersions(povView.getPurchaseOrderCostVersions());
				}
			}
			
		} catch (PurchaseProgramException ppe) {
			throw ppe;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw ppe;
		}
		
		return insertedPovId;
	}
	
	public int updatePurchaseOrderVersion(PurchaseOrderVersionView povView) throws PurchaseProgramException{
		int updatedPovs = -1;
		PurchaseOrderVersionDTO povDTO = null;
		PurchaseOrderVersionDAO povDAO = null;
		PurchaseOrderCostVersionService pcvService = null;
		
		try{
			
			if (validateData(povView)){
				povDAO = new PurchaseOrderVersionDAO();
				pcvService = new PurchaseOrderCostVersionService();
				povDTO = PurchaseOrderVersionConverter.convert(povView);
				updatedPovs = povDAO.updatePurchaseOrderVersionById(povDTO);
				
				if (CollectionUtils.isNotEmpty(povView.getPurchaseOrderCostVersions())){
					
					for (Iterator itr = povView.getPurchaseOrderCostVersions().iterator(); itr.hasNext(); ){
						pcvService.updatePurchaseOrderCostVersion((PurchaseOrderCostVersionView) itr.next());
					}
				}
			}
			
		} catch (PurchaseProgramException ppe) {
			throw ppe;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw ppe;
		}
		
		return updatedPovs;
	}
	public List getPurchaseOrderVersions(int orderField, boolean asc) throws PurchaseProgramException{
		PurchaseOrderVersionView purchaseOrderVersionView = null;
		BiWeekPeriodVersionView biWeekPeriodVersionView = null;
		PlantView plantView = null;
		PurchaseOrderVersionDAO purchaseOrderVersionDAO = null;
		BiWeekPeriodVersionService biWeekPeriodVersionService = null;
		PlantService plantService = null;
		PurchaseOrderVersionComparator purchaseOrderVersionComparator = null;
		List purchaseOrderVersionsDTO = null;
		List purchaseOrderVersionsView = null;
		
		try{
			
			purchaseOrderVersionsView = new ArrayList(0);
			purchaseOrderVersionDAO = new PurchaseOrderVersionDAO();
			purchaseOrderVersionComparator = new PurchaseOrderVersionComparator();
			
			purchaseOrderVersionsDTO = purchaseOrderVersionDAO.getPurchaseOrderVersions();
			
			if (CollectionUtils.isNotEmpty(purchaseOrderVersionsDTO)){
				biWeekPeriodVersionService = new BiWeekPeriodVersionService();
				plantService = new PlantService();
				
				purchaseOrderVersionsView = PurchaseOrderVersionConverter.convertDTOList(purchaseOrderVersionsDTO);
				
				for (Iterator itr = purchaseOrderVersionsView.iterator(); itr.hasNext(); ){
					purchaseOrderVersionView = (PurchaseOrderVersionView) itr.next();
					
					if (purchaseOrderVersionView.getBiWeekPeriodVersionId() != null){
						biWeekPeriodVersionView = biWeekPeriodVersionService.getBiWeekPeriodVersion(purchaseOrderVersionView.getBiWeekPeriodVersionId());
						
						purchaseOrderVersionView.setBiWeekPeriodVersionView(biWeekPeriodVersionView);
					}
					
					if (purchaseOrderVersionView.getPlantId() != null){
						plantView = new PlantView();
						
						plantView.setPlantId(purchaseOrderVersionView.getPlantId().intValue());
						
						plantView = plantService.getPlant(plantView);
						
						purchaseOrderVersionView.setPlantView(plantView);
					}
				}
				
				purchaseOrderVersionComparator.setOrderField(orderField);
				purchaseOrderVersionComparator.setOrderAsc(asc);
				
				Collections.sort(purchaseOrderVersionsView, purchaseOrderVersionComparator);
			}
			
		} catch (PurchaseProgramException ppe) {
			throw ppe;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw ppe;
		}
		
		return purchaseOrderVersionsView;
	}
	
	public List getPurchaseOrderVersionsByVersion(Integer versionId, Integer transportId, Integer isOrderPurchaseOrTransit, int orderField, boolean asc) throws PurchaseProgramException{
		PurchaseOrderVersionDAO purchaseOrderVersionDAO = null;
		List purchaseOrderVersionsDTO = null;
		List purchaseOrderVersionsView = null;
		try{
			if (versionId != null){
				purchaseOrderVersionDAO = new PurchaseOrderVersionDAO();
				purchaseOrderVersionsDTO = purchaseOrderVersionDAO.getPurchaseOrderVersionByBiWeekPeriodVersionId(versionId, transportId, isOrderPurchaseOrTransit, orderField, asc);
				if (CollectionUtils.isNotEmpty(purchaseOrderVersionsDTO)){
					purchaseOrderVersionsView = PurchaseOrderVersionConverter.convertDTOList(purchaseOrderVersionsDTO);
				}
				setAdditionalData(purchaseOrderVersionsView, versionId.intValue());
			}
		} catch(PurchaseProgramException ppe){
			throw ppe;
		} catch(BiWeekException bwe){
			PurchaseProgramException ppe = new PurchaseProgramException(bwe, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_LISTS);
			ppe.printStackTrace(System.out);
			throw ppe;
		} catch(BlockDistributionException blockDistributionException){
			PurchaseProgramException ppe = new PurchaseProgramException(blockDistributionException, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_LISTS);
			ppe.printStackTrace(System.out);
			throw ppe;
		} catch(TagDistributionException tagDistributionException){
			PurchaseProgramException ppe = new PurchaseProgramException(tagDistributionException, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_LISTS);
			ppe.printStackTrace(System.out);
			throw ppe;
		} catch(PlantException pe){
			PurchaseProgramException ppe = new PurchaseProgramException(pe, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_LISTS);
			ppe.printStackTrace(System.out);
			throw ppe;
		} catch(TransportException tre){
			PurchaseProgramException ppe = new PurchaseProgramException(tre, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_SELECT);
			ppe.printStackTrace(System.out);
			throw ppe;
		}
		return purchaseOrderVersionsView;
	}
	
	public PurchaseOrderVersionView getPurchaseOrderVersion(Integer povId) throws PurchaseProgramException{
		PurchaseOrderVersionView povView = null;
		PurchaseOrderVersionDTO povDTO = null;
		PurchaseOrderVersionDAO povDAO = null;
		List povListView = null;
		
		try{
			
			if (povId != null){
				povDAO = new PurchaseOrderVersionDAO();
				
				povDTO = povDAO.getPurchaseOrderVersionById(povId);
				
				if (povDTO != null){
					povListView = new ArrayList(1);
					povView = PurchaseOrderVersionConverter.convert(povDTO);
					
					povListView.add(povView);
					
					setAdditionalData(povListView,0);				
				}
			}
			
		} catch(PurchaseProgramException ppe){
			throw ppe;
		} catch(BlockDistributionException blockDistributionException){
			PurchaseProgramException ppe = new PurchaseProgramException(blockDistributionException, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_LISTS);
			ppe.printStackTrace(System.out);
			throw ppe;
		} catch(TagDistributionException tagDistributionException){
			PurchaseProgramException ppe = new PurchaseProgramException(tagDistributionException, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_LISTS);
			ppe.printStackTrace(System.out);
			throw ppe;
		} catch(BiWeekException bwe){
			PurchaseProgramException ppe = new PurchaseProgramException(bwe, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_SELECT);
			ppe.printStackTrace(System.out);
			throw ppe;
		} catch(PlantException pe){
			PurchaseProgramException ppe = new PurchaseProgramException(pe, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_SELECT);
			ppe.printStackTrace(System.out);
			throw ppe;
		} catch(TransportException tre){
			PurchaseProgramException ppe = new PurchaseProgramException(tre, PurchaseProgramException.LAYER_SERVICE, PurchaseProgramException.ACTION_SELECT);
			ppe.printStackTrace(System.out);
			throw ppe;
		}
		
		return povView;
	}
	
	public int deletePurchaseOrderVersion(int versionId, int purchaseOrderVersionId) throws PurchaseProgramException{
		int deletedPovs = -1;
		PurchaseOrderVersionDAO povDAO = null;
		try{
			povDAO = new PurchaseOrderVersionDAO();
			povDAO.deletePurchaseOrderVersionById(versionId, purchaseOrderVersionId);
		} catch (PurchaseProgramException ppe) {
			throw ppe;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw ppe;
		}
		
		return deletedPovs;
	}
	
	public void deleteBalancesVersion(int purchaseOrderVersionId)throws PurchaseProgramException{
		PurchaseOrderVersionDAO purchaseOrderVersionDAO = null;
		try{
			purchaseOrderVersionDAO = new PurchaseOrderVersionDAO();
			purchaseOrderVersionDAO.deleteBalancesVersion(purchaseOrderVersionId);
		} catch (PurchaseProgramException purchaseProgramException) {
			throw purchaseProgramException;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw ppe;
		}
	}
	
	public void addBalacesVersion(int versionId, int purchaseOrderVersionId, int months)throws PurchaseProgramException{
		PurchaseOrderVersionDAO purchaseOrderVersionDAO = null;
		MonthsPeriodsDAO monthsPeriodsDAO = null;
		List monthsPeriodsDTOs = null;
		MonthsPeriodsForVersionDTO monthsPeriodsForVersionDTO = null;
		double amount = 0;
		Iterator monthsPeriodsForVersionDTOIterator = null;
		try{
			purchaseOrderVersionDAO = new PurchaseOrderVersionDAO();
			monthsPeriodsDAO = new MonthsPeriodsDAO();
			monthsPeriodsDTOs = monthsPeriodsDAO.monthsToDraftDiscount(versionId, purchaseOrderVersionId, months);
			amount = purchaseOrderVersionDAO.getDraftDiscount(purchaseOrderVersionId, months);
			monthsPeriodsForVersionDTOIterator = monthsPeriodsDTOs.iterator();
			while(monthsPeriodsForVersionDTOIterator.hasNext()){
				monthsPeriodsForVersionDTO = (MonthsPeriodsForVersionDTO)monthsPeriodsForVersionDTOIterator.next();
				purchaseOrderVersionDAO.insertBalancesVersion(monthsPeriodsForVersionDTO.getMonthPeriodId(), monthsPeriodsForVersionDTO.getMonthPeriodVersionId(), purchaseOrderVersionId, amount);
			}
		} catch (PurchaseProgramException purchaseProgramException) {
			throw purchaseProgramException;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw ppe;
		}
	}
	
	private void setAdditionalData(List povViews, int versionId) throws BiWeekException, PlantException, TransportException, BlockDistributionException, TagDistributionException{
		PlantView plantView = null;
		PurchaseOrderVersionView purchaseOrderVersionView = null; 
		BiWeekPeriodVersionService biWeekPeriodVersionService = null;
		PlantService plantService = null;
		TransportService transportService = null;
		
		TagDistributionService tagDistributionService = null;
		TagDistributionView tagDistributionView = null;
				
		if (CollectionUtils.isNotEmpty(povViews)){
			plantView = new PlantView();
			biWeekPeriodVersionService = new BiWeekPeriodVersionService();
			plantService = new PlantService();
			transportService = new TransportService();
			
			tagDistributionService = new TagDistributionService();
			
			for (Iterator itr = povViews.iterator(); itr.hasNext(); ){
				purchaseOrderVersionView = (PurchaseOrderVersionView) itr.next();
				tagDistributionView = null;
				if( versionId > 0 ){
					if(purchaseOrderVersionView.getPurchaseOrderVersionId().intValue() >0){
						tagDistributionView = tagDistributionService.getTagDistributionByVersion(versionId,purchaseOrderVersionView.getPurchaseOrderVersionId().intValue());
						purchaseOrderVersionView.setTagDistributionView(tagDistributionView);
					}
				}else{				
					if ((purchaseOrderVersionView.getBiWeekPeriodVersionView() == null) && (purchaseOrderVersionView.getBiWeekPeriodVersionId() != null)){
						purchaseOrderVersionView.setBiWeekPeriodVersionView(biWeekPeriodVersionService.getBiWeekPeriodVersion(purchaseOrderVersionView.getBiWeekPeriodVersionId()));
					}
					if ((purchaseOrderVersionView.getPlantView() == null) && (purchaseOrderVersionView.getPlantId() != null)){
						plantView.setPlantId(purchaseOrderVersionView.getPlantId().intValue());
						purchaseOrderVersionView.setPlantView(plantService.getPlant(plantView));
					}
					if ((purchaseOrderVersionView.getTransportView() == null) && (purchaseOrderVersionView.getTransportId() != null)){
						purchaseOrderVersionView.setTransportView(transportService.getTransport(purchaseOrderVersionView.getTransportId().intValue()));
					}
				}
			}
		}
	}
	
	private boolean validateData(PurchaseOrderVersionView povView){
		boolean validData = false;
		
		if ((povView != null) && (povView.getTransportId() != null) && (povView.getBiWeekPeriodVersionId() != null) &&
			(StringUtils.isNotEmpty(povView.getCode())) && (povView.getCapacity() != null)){
			
			validData = true;
		}
		
		return validData;
	}
	/**
	 * Este metodo obtiene una lista para
	 * la busqueda de Ordenes de Compra de Version
	 * 
	 * @author Edgar Joao
	 * @param filterSearch
	 * @return
	 * @throws PurchaseProgramException
	 */
	public List getPurchasesOrdersVersionsList(ManualChargesFilterSearchView filterSearch, int versionId) throws PurchaseProgramException{
		PurchaseOrderVersionDAO dao = null;	
		PurchaseOrderVersionConverter converter = null;
		List list = null;
		Integer transportIdFilter = null;
		Integer transitTypeFilter = null;
		try{
			dao = new PurchaseOrderVersionDAO();
			list = new ArrayList(0);
			converter = new PurchaseOrderVersionConverter();
			if(filterSearch.getTransportIdFilter() > -1){
				transportIdFilter = new Integer(filterSearch.getTransportIdFilter());
			}
			if(filterSearch.getTransitTypeFilter() > -1){
				transitTypeFilter = new Integer(filterSearch.getTransitTypeFilter());
			}							
			list = converter.convertPurchasesOrdersViewList(dao.getPurchasesOrdersVersionsList(versionId, transportIdFilter, transitTypeFilter, new Integer(filterSearch.getInitPeriodFilter()), new Integer(filterSearch.getEndPeriodFilter())));		
		} catch (PurchaseProgramException purchaseProgramException) {
			throw purchaseProgramException;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw ppe;
		}			
		return list;
	}
	/**
	 * Metodo que obtiene ordenes de compra
	 * @author Edgar Joao
	 * @param versionId ID de la Version
	 * @param purchaseOrderIdsList Lista de los Ids de Ordenes de Compra
	 * @return
	 * @throws PurchaseProgramException
	 */
	public List getPurchasesOrdersVersionsListById(int versionId, int manualChargeId) throws PurchaseProgramException{
		PurchaseOrderVersionDAO dao = null;	
		PurchaseOrderVersionConverter converter = null;
		List list = null;		
		try{
			dao = new PurchaseOrderVersionDAO();
			list = new ArrayList(0);
			converter = new PurchaseOrderVersionConverter();			
			list = converter.convertPurchasesOrdersViewList(dao.getPurchasesOrdersVersionsListById(versionId, manualChargeId));		
		} catch (PurchaseProgramException purchaseProgramException) {
			throw purchaseProgramException;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw ppe;
		}			
		return list;
	}
	/**
	 * Metodo que obtiene ordenes de compra por mes
	 * @param versionId ID de la Version
	 * @param futurePeriodVersionId id de mes para la version
	 * @return Lista de los Ids de Ordenes de Compra
	 * @throws PurchaseProgramException
	 */
	public List purchasesOrdersVersionsByMonth(int versionId, int futurePeriodVersionId) throws PurchaseProgramException{
		PurchaseOrderVersionDAO dao = null;	
		PurchaseOrderVersionConverter converter = null;
		List list = null;		
		try{
			dao = new PurchaseOrderVersionDAO();
			list = new ArrayList(0);
			converter = new PurchaseOrderVersionConverter();			
			list = converter.convertPurchasesOrdersViewList(dao.purchasesOrdersVersionsByFuture(versionId, futurePeriodVersionId), false);
		} catch (PurchaseProgramException purchaseProgramException) {
			throw purchaseProgramException;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw ppe;
		}			
		return list;
	}
	
	public void moveSort(int purchaseOrderVersionId, int direction)throws PurchaseProgramException{
		PurchaseOrderVersionDAO purchaseOrderVersionDAO = null;
		try{
			purchaseOrderVersionDAO = new PurchaseOrderVersionDAO();
			purchaseOrderVersionDAO.moveSort(purchaseOrderVersionId, direction);
		} catch (PurchaseProgramException purchaseProgramException) {
			throw purchaseProgramException;
		} catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException = new PurchaseProgramException(exception, CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw purchaseProgramException;
		}
	}
	
	public void originalSort(int futurePeriodVersionId)throws PurchaseProgramException{
		PurchaseOrderVersionDAO purchaseOrderVersionDAO = null;
		try{
			purchaseOrderVersionDAO = new PurchaseOrderVersionDAO();
			purchaseOrderVersionDAO.originalSort(futurePeriodVersionId);
		} catch (PurchaseProgramException purchaseProgramException) {
			throw purchaseProgramException;
		} catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException = new PurchaseProgramException(exception, CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw purchaseProgramException;
		}
	}
	
	public void saveSort(int futurePeriodVersionId, List purchaseOrderVersions)throws PurchaseProgramException{
		PurchaseOrderVersionDAO purchaseOrderVersionDAO = null;
		Iterator iterator = purchaseOrderVersions.iterator();
		PurchaseOrderVersionView purchaseOrderVersionView = null;
		int number = -1;
		try{
			purchaseOrderVersionDAO = new PurchaseOrderVersionDAO();
			while(iterator.hasNext()){
				purchaseOrderVersionView = (PurchaseOrderVersionView)iterator.next();
				if( purchaseOrderVersionView.getFutureSort() != purchaseOrderVersionView.getOriginalFutureSort() ){
					if( StringUtils.isNotEmpty(purchaseOrderVersionView.getFutureSort())){
						number = Integer.parseInt(purchaseOrderVersionView.getFutureSort());
					}else{
						number = 999;
					}
					purchaseOrderVersionDAO.saveSort(futurePeriodVersionId, purchaseOrderVersionView.getPurchaseOrderVersionId().intValue(), number);
				}
			}
			purchaseOrderVersionDAO.setSort(futurePeriodVersionId);
		} catch (PurchaseProgramException purchaseProgramException) {
			throw purchaseProgramException;
		} catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException = new PurchaseProgramException(exception, CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw purchaseProgramException;
		}
	}
}